<?php
/**
 * Created by PhpStorm.
 * User: 钊鑫
 * Date: 2019/1/28
 * Time: 10:21
 */
namespace app\api\validate;

use app\api\model\Homeuser as HomeuserModel;

class Homeuser extends Base
{
    public $id = '';

    protected $rule = [
        'type'              =>  'require|number',
        'img'               => 'require',
        'username'          =>  'require|checkname',
        'college'           =>  'require',
        'email'             =>  'require|email',
        'number'            =>  'require|mobile|checknumber',
    ];

    protected $message = [
        'type.require'                  =>  '用户身份不能为空！',
        'type.number'                   =>  '用户身份输入有误！',
        'img.require'                   =>  '请选择上传的图片',
        'username.require'              =>  '该用户名不能为空！',
        'username.checkname'            =>  '该用户名已存在！',
        'student_id.require'            =>  '学号不能为空！',
        'student_id.number'             =>  '请输入有效的学号！',
        'college.require'               =>  '请选择所属院系！',
        'class.require'                 =>  '请选择所属专业',
        'email.require'                 =>  '邮箱不能为空！',
        'email.email'                   =>  '请输入有效的邮箱！',
        'number.require'                =>  '手机号不能为空！',
        'number.checknumber'            =>  '手机号已存在，请重新输入！',
        'number.mobile'                 =>  '请输入有效的手机号',
    ];

    public function __construct($id = '')
    {
        $this->id = $id;
    }

    // edit 验证场景定义
    public function sceneStudent()
    {
        $this->append('student_id', 'require|number')
            ->append('class', 'require|number');
    }

    //用户名验证
    protected function checkname($value){
        $result = HomeuserModel::where('id','<>',$this->id)->where('username',$value)->count();
        return ($result == 0) ? true : false;
    }

    //手机号验证
    protected function checknumber($value){
        $result = HomeuserModel::where('id','<>',$this->id)->where('number',$value)->count();
        return ($result == 0) ? true : false;
    }

}